{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Remove Duplicates from Sorted Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: removeDuplicates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #删除有序数组中的重复项"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个 <strong>非严格递增排列</strong> 的数组 <code>nums</code> ，请你<strong><a href=\"http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\"> 原地</a></strong> 删除重复出现的元素，使每个元素 <strong>只出现一次</strong> ，返回删除后数组的新长度。元素的 <strong>相对顺序</strong> 应该保持 <strong>一致</strong> 。然后返回 <code>nums</code> 中唯一元素的个数。</p>\n",
    "\n",
    "<p>考虑 <code>nums</code> 的唯一元素的数量为 <code>k</code> ，你需要做以下事情确保你的题解可以被通过：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>更改数组 <code>nums</code> ，使 <code>nums</code> 的前 <code>k</code> 个元素包含唯一元素，并按照它们最初在 <code>nums</code> 中出现的顺序排列。<code>nums</code>&nbsp;的其余元素与 <code>nums</code> 的大小不重要。</li>\n",
    "\t<li>返回 <code>k</code>&nbsp;。</li>\n",
    "</ul>\n",
    "\n",
    "<p><strong>判题标准:</strong></p>\n",
    "\n",
    "<p>系统会用下面的代码来测试你的题解:</p>\n",
    "\n",
    "<pre>\n",
    "int[] nums = [...]; // 输入数组\n",
    "int[] expectedNums = [...]; // 长度正确的期望答案\n",
    "\n",
    "int k = removeDuplicates(nums); // 调用\n",
    "\n",
    "assert k == expectedNums.length;\n",
    "for (int i = 0; i &lt; k; i++) {\n",
    "    assert nums[i] == expectedNums[i];\n",
    "}</pre>\n",
    "\n",
    "<p>如果所有断言都通过，那么您的题解将被 <strong>通过</strong>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong class=\"example\">示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,1,2]\n",
    "<strong>输出：</strong>2, nums = [1,2,_]\n",
    "<strong>解释：</strong>函数应该返回新的长度 <strong><code>2</code></strong> ，并且原数组 <em>nums </em>的前两个元素被修改为 <strong><code>1</code></strong>, <strong><code>2 </code></strong><code>。</code>不需要考虑数组中超出新长度后面的元素。\n",
    "</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [0,0,1,1,1,2,2,3,3,4]\n",
    "<strong>输出：</strong>5, nums = [0,1,2,3,4]\n",
    "<strong>解释：</strong>函数应该返回新的长度 <strong><code>5</code></strong> ， 并且原数组 <em>nums </em>的前五个元素被修改为 <strong><code>0</code></strong>, <strong><code>1</code></strong>, <strong><code>2</code></strong>, <strong><code>3</code></strong>, <strong><code>4</code></strong> 。不需要考虑数组中超出新长度后面的元素。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>nums</code> 已按 <strong>非严格递增</strong>&nbsp;排列</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [remove-duplicates-from-sorted-array](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [remove-duplicates-from-sorted-array](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,1,2]', '[0,0,1,1,1,2,2,3,3,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        i = 2\n",
    "        for n in nums[2:]:\n",
    "            if n != nums[i-2]:\n",
    "                nums[i] = n\n",
    "                i += 1\n",
    "        return i\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "#         i = 0\n",
    "#         for e in nums:\n",
    "#             if i < 2 or e != nums[i - 2]:\n",
    "#                 nums[i] = e\n",
    "#                 i += 1\n",
    "#             else:\n",
    "#                 continue\n",
    "#         return i\n",
    "        \n",
    "        i = 0\n",
    "        j = 2\n",
    "        while j < len(nums):\n",
    "            if nums[i] == nums[j]:\n",
    "                del nums[j]\n",
    "            else:\n",
    "                i += 1\n",
    "                j += 1\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        lastNum = None\n",
    "        appearance = 0\n",
    "        j = 0\n",
    "        for num in nums:\n",
    "            if num != lastNum:\n",
    "                lastNum = num\n",
    "                appearance = 1\n",
    "                nums[j] = lastNum\n",
    "                j += 1\n",
    "            elif appearance == 1:\n",
    "                appearance += 1\n",
    "                nums[j] = lastNum\n",
    "                j += 1\n",
    "        return j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        length = len(nums)\n",
    "        if length <= 2:\n",
    "            return length\n",
    "        sorted(nums)\n",
    "        print(nums)\n",
    "        i = 0\n",
    "        j = 0\n",
    "        last_num = None\n",
    "        last_num_count = 0\n",
    "        while j < length:\n",
    "            if nums[j] != last_num or (nums[j] == last_num and last_num_count <= 1):\n",
    "                last_num_count = 1 if nums[j] != last_num else last_num_count + 1\n",
    "                last_num = nums[j]\n",
    "                print(nums[j])\n",
    "                nums[i] = nums[j]\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                last_num = nums[j]\n",
    "                last_num_count += 1\n",
    "                j += 1\n",
    "        print(nums)\n",
    "        return i\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        i = 0\n",
    "        while i < len(nums):\n",
    "            j = i+1\n",
    "            flag = True\n",
    "            while j < len(nums):          \n",
    "                if nums[i]==nums[j]:\n",
    "                    if flag == True:\n",
    "                        j+=1\n",
    "                        flag = False\n",
    "                    else:\n",
    "                        nums.pop(j)\n",
    "                else:\n",
    "                    break\n",
    "            i=j\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        tag = []\n",
    "        if not nums:return 0\n",
    "        for j in range(1,len(nums)):\n",
    "            if j>1 and nums[j]==nums[j-1] and nums[j]==nums[j-2]:\n",
    "                tag.append(j)\n",
    "        for j in tag[::-1]:\n",
    "            nums.remove(nums[j])\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "#         if not nums:return\n",
    "#         newTail = 0\n",
    "#         cishu = 0\n",
    "#         for num in nums[1:]:\n",
    "#             if num == nums[newTail]:\n",
    "#                 cishu += 1\n",
    "#                 if cishu==1:\n",
    "#                     newTail+=1\n",
    "#                     nums[newTail] = num\n",
    "                    \n",
    "                    \n",
    "#                 else:\n",
    "#                     cishu = 1\n",
    "                    \n",
    "                        \n",
    "#             if num != nums[newTail] :\n",
    "#                 newTail+=1\n",
    "#                 nums[newTail] = num\n",
    "#                 cishu = 0\n",
    "#         return newTail+1\n",
    "\n",
    "        i = 0\n",
    "        for num in nums:\n",
    "            if i < 2 or num > nums[i-2]:\n",
    "                nums[i] = num\n",
    "                i += 1\n",
    "        return i  \n",
    "    \n",
    "   \n",
    "    \n",
    "    \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        j = 0\n",
    "        a = len(nums)\n",
    "        c = 0\n",
    "        i = 1\n",
    "        if a > 1:\n",
    "            while i < a:\n",
    "                    if nums[i]!=nums[j]:\n",
    "                            j = j+1\n",
    "                            nums[j] = nums[i]\n",
    "                            c = 0\n",
    "                    else:\n",
    "                        if c < 1:\n",
    "                            j = j+1\n",
    "                            nums[j] = nums[i]\n",
    "                            c = c + 1\n",
    "                    i = i+1\n",
    "            return j+1\n",
    "        else:\n",
    "            return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not nums:\n",
    "            return 0\n",
    "        \n",
    "        last, i, same = 0, 1, False\n",
    "        while i < len(nums):\n",
    "            if nums[last] != nums[i] or not same:\n",
    "                same = nums[last] == nums[i]\n",
    "                last += 1\n",
    "                nums[last] = nums[i]\n",
    "            i += 1\n",
    "        return last + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n1,n2 = 0,0\n",
    "        aa = \"\"\n",
    "        while True:\n",
    "            try:\n",
    "                nums[n1]\n",
    "            except IndexError:\n",
    "                break\n",
    "            if nums[n1] != aa:\n",
    "                aa = nums[n1]\n",
    "                n2 = 1\n",
    "                n1 += 1\n",
    "            else:\n",
    "                if n2 == 2:\n",
    "                    nums.pop(n1)\n",
    "                else:\n",
    "                    n2 += 1\n",
    "                    n1 += 1\n",
    "        return(len(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if len(nums) < 3:\n",
    "            return len(nums)\n",
    "        position=left=0\n",
    "        right = 2\n",
    "        while right < len(nums):\n",
    "            if nums[left]==nums[right]:\n",
    "                left+=1\n",
    "                right+=1\n",
    "            else:\n",
    "                nums[position] = nums[left]\n",
    "                position+=1\n",
    "                left+=1\n",
    "                right+=1\n",
    "        # 最后两个元素是有效的\n",
    "        nums[position] = nums[left]\n",
    "        position+=1\n",
    "        nums[position] = nums[left+1]\n",
    "        return position+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if not nums:\n",
    "            return None\n",
    "        i, sub_length = 1, 1\n",
    "        while i < len(nums):\n",
    "            if nums[i] == nums[i - 1]:\n",
    "                if sub_length == 2:\n",
    "                    del nums[i]\n",
    "                else:\n",
    "                    sub_length += 1\n",
    "                    i += 1\n",
    "            else:\n",
    "                sub_length = 1\n",
    "                i += 1\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        # 暴力解法,hash表\n",
    "        # 双指针解法，单一元素\n",
    "        p = 0\n",
    "        q = 1\n",
    "        n = len(nums)\n",
    "        if not n :\n",
    "            return 0\n",
    "        \n",
    "        while q < len(nums):\n",
    "            # print(p,q)\n",
    "            if nums[p] == nums[q] and (q - p < 2):\n",
    "                q += 1\n",
    "            elif nums[p] == nums[q] and (q - p >= 2):\n",
    "                nums.pop(q)\n",
    "            elif nums[p] < nums[q]:\n",
    "                p = q\n",
    "                q += 1\n",
    "        # print(nums)\n",
    "        return len(nums) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        i = -1\n",
    "        j = 0\n",
    "        if n < 2:\n",
    "            return n\n",
    "        while j < n:\n",
    "            if j < 2:\n",
    "                i += 1\n",
    "                nums[i] = nums[j]\n",
    "                j += 1\n",
    "            else:\n",
    "                if nums[j] == nums[i] and nums[j] == nums[i-1]:\n",
    "                    j += 1\n",
    "                else:\n",
    "                    i += 1\n",
    "                    nums[i] = nums[j]\n",
    "                    j += 1\n",
    "        return i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 0:\n",
    "            return 0\n",
    "\n",
    "        count = 1\n",
    "        i = 1\n",
    "\n",
    "        for j in range(1, n):\n",
    "            if nums[j] == nums[j - 1]:\n",
    "                count += 1\n",
    "\n",
    "                if count > 2:\n",
    "                    # igore the triple num\n",
    "                    continue\n",
    "                nums[i] = nums[j]\n",
    "                i += 1\n",
    "            else:\n",
    "                count = 1\n",
    "                nums[i] = nums[j]\n",
    "                i += 1\n",
    "        \n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        i = 0\n",
    "        for num in nums:\n",
    "            if i < 2 or num != nums[i - 2]:\n",
    "                nums[i] = num\n",
    "                i += 1\n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        i, j, n = 1, 2, len(nums)\n",
    "        while j < n:\n",
    "            if nums[j] == nums[i] and nums[j] == nums[i - 1]:\n",
    "                pass\n",
    "            else:\n",
    "                nums[i + 1] = nums[j]\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return i + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        l = len(nums)\n",
    "        if l <= 2:\n",
    "            return l\n",
    "        counter = 2\n",
    "        for i in range(2, l):\n",
    "            print(counter)\n",
    "            if nums[i] != nums[counter - 2]:\n",
    "                \n",
    "                nums[counter] = nums[i]\n",
    "                counter += 1\n",
    "        return counter\n",
    "        \n",
    "\n",
    "            \n",
    "\n",
    "            \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if len(nums)<3:\n",
    "            return len(nums)\n",
    "        readp = 1\n",
    "        writep = 1\n",
    "        prev = nums[0]\n",
    "        two = False\n",
    "        while readp<len(nums):\n",
    "            if two==False and nums[readp]==prev:\n",
    "                print(readp,writep,prev)\n",
    "                two = True\n",
    "                nums[writep] = nums[readp]\n",
    "                readp += 1\n",
    "                writep += 1\n",
    "            elif two==True and nums[readp]==prev:\n",
    "                print(readp,writep,prev)\n",
    "                readp += 1\n",
    "            else:\n",
    "                print(readp,writep,prev)\n",
    "                nums[writep] = nums[readp]\n",
    "                prev=nums[writep]\n",
    "                two = False\n",
    "                readp+=1\n",
    "                writep+=1\n",
    "        return writep\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if not nums or len(nums)==0:\n",
    "            return 0\n",
    "        length=len(nums)\n",
    "        if length<3:\n",
    "            return length\n",
    "        j=length-1\n",
    "        temp=0\n",
    "        while j>0:\n",
    "            num=0\n",
    "            for i in range(j-1,-1,-1):\n",
    "                if nums[i]==nums[j]:\n",
    "                    num+=1\n",
    "                    temp=i\n",
    "            print(num)\n",
    "\n",
    "            if num>=2:\n",
    "                while num>1:\n",
    "                    nums[temp]=None\n",
    "                    temp+=1\n",
    "                    num-=1\n",
    "\n",
    "                j=temp-1\n",
    "            else:\n",
    "                j-=1\n",
    "        count=0\n",
    "        for i in range(length):\n",
    "            if nums[i]==None:\n",
    "                count+=1\n",
    "        if count>0:\n",
    "           for i in range(count):\n",
    "               nums.remove(None)\n",
    "\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        slow = 0\n",
    "        for fast in range(len(nums)):\n",
    "            if slow < 2 or nums[fast] != nums[slow - 2]:\n",
    "                nums[slow] = nums[fast]\n",
    "                slow += 1\n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        slow = 0\n",
    "        for fast in range(len(nums)):\n",
    "            if slow < 2 or nums[fast] != nums[slow - 2]:\n",
    "                #对于第三个元素及之后的元素，如果这个元素与它前面的两个元素不相同，说明它出现的次数没有超过两次，放到slow这个位置；\n",
    "                nums[slow] = nums[fast]\n",
    "                slow += 1\n",
    "            # 如果这个元素与它前面的两个元素相同，说明它出现的次数已经超过两次，我们就将它跳过，继续遍历后面的元素。\n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n <=2:\n",
    "            return n\n",
    "        i = 1\n",
    "        for j in range(2,n):\n",
    "            if nums[j]!= nums[i-1]:\n",
    "                nums[i+1] = nums[j]\n",
    "                i+=1\n",
    "        return i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\r\n",
    "    def removeDuplicates(self, nums):\r\n",
    "        slow = 0\r\n",
    "        for fast in range(len(nums)):\r\n",
    "            if slow < 2 or nums[fast] != nums[slow - 2]:\r\n",
    "                nums[slow] = nums[fast]\r\n",
    "                slow += 1\r\n",
    "        return slow\r\n",
    "\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        for i,item in enumerate(collections.Counter(nums)):\n",
    "            while nums.count(item) > 2:\n",
    "                nums.remove(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        nlen=len(nums)\n",
    "        def remove(pre,cur):\n",
    "            if cur==nlen:\n",
    "                return pre+1\n",
    "            else:\n",
    "                if cur==nlen-1:\n",
    "                    nums[pre+1]=nums[cur]\n",
    "                    return remove(pre+1,cur+1)\n",
    "                elif cur==nlen-2:\n",
    "                    nums[pre+1]=nums[cur]\n",
    "                    return remove(pre+1,cur+1)\n",
    "                else:\n",
    "                    if nums[cur]==nums[cur+1] and nums[cur]==nums[cur+2]:\n",
    "                        nums[pre+1]=nums[cur]\n",
    "                        nums[pre+2]=nums[cur]\n",
    "                        pre+=2;cur+=1\n",
    "                        while cur<nlen and nums[pre]==nums[cur]:\n",
    "                            cur+=1\n",
    "                        return remove(pre,cur)\n",
    "                    else:\n",
    "                         nums[pre+1]=nums[cur]\n",
    "                         return remove(pre+1,cur+1)\n",
    "\n",
    "        return remove(-1,0)\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        l=len(nums)\n",
    "        p1,p2=0,1\n",
    "        c,result=1,l\n",
    "        ite=1\n",
    "        while ite<l:\n",
    "            ite+=1\n",
    "            if nums[p2]==nums[p1]:\n",
    "                c=c+1\n",
    "                if c>2:\n",
    "                    for i in range(p2,l-1):\n",
    "                        nums[i]=nums[i+1]\n",
    "                    result-=1\n",
    "                    c=2\n",
    "                else: \n",
    "                    p2+=1\n",
    "            else:\n",
    "                c=1\n",
    "                p1=p2\n",
    "                p2+=1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n <=2: return n\n",
    "\n",
    "        slow, fast = 2, 2\n",
    "        while fast < n:\n",
    "            if nums[fast] != nums[slow-2]:\n",
    "                nums[slow] = nums[fast]\n",
    "                slow +=1\n",
    "            fast +=1\n",
    "        \n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        slow = 1\n",
    "        length = 1\n",
    "        sums = 1\n",
    "        old_num = nums[0]\n",
    "        for i, num in enumerate(nums[1:]):\n",
    "            if num == old_num and sums == 2:\n",
    "                continue\n",
    "            elif num == old_num and sums == 1:\n",
    "                nums[slow] = num\n",
    "                slow += 1\n",
    "                length += 1\n",
    "                sums += 1\n",
    "            else:\n",
    "                nums[slow] = num\n",
    "                old_num = num\n",
    "                slow += 1\n",
    "                length += 1\n",
    "                sums = 1\n",
    "        return length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        pt = 1\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] != nums[i-1]:\n",
    "                nums[pt] = nums[i]\n",
    "                pt += 1\n",
    "        return pt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \n",
    "        \"\"\"\n",
    "        if not nums:\n",
    "            return 0\n",
    "        old = 0\n",
    "        new = 0\n",
    "        while old<len(nums)-1:\n",
    "            if nums[old+1]!=nums[old]:\n",
    "                nums[new+1]=nums[old+1]\n",
    "                new = new+1\n",
    "            old = old+1\n",
    "    #     print(nums)\n",
    "        return new+1\n",
    "        \n",
    "        # if not nums:\n",
    "        #     return 0\n",
    "        # old = 1\n",
    "        # new = 1\n",
    "        # while old<len(nums):\n",
    "        #     if nums[old-1]!=nums[old]:\n",
    "        #         nums[new]=nums[old]\n",
    "        #         new = new+1\n",
    "        #     old = old+1\n",
    "        # return new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: 'List[int]') -> 'int':\n",
    "        # if len(nums) <= 1:\n",
    "        #     return len(nums)\n",
    "        # lo = 0\n",
    "        # hi = len(nums) - 1\n",
    "        # cnt = 0\n",
    "        # while lo < hi:\n",
    "        #     while lo < hi and nums[lo] == nums[lo+1]:\n",
    "        #         lo += 1\n",
    "        #     while lo < hi and nums[hi] == nums[hi-1]:\n",
    "        #         hi -= 1\n",
    "        #     cnt += 2\n",
    "        #     lo += 1\n",
    "        #     hi -= 1\n",
    "        # return cnt\n",
    "        if not nums:\n",
    "            return 0\n",
    "\n",
    "        newTail = 0\n",
    "\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] != nums[newTail]:\n",
    "                newTail += 1\n",
    "                nums[newTail] = nums[i]\n",
    "\n",
    "        return newTail + 1\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        # return len(set(nums))\n",
    "        count = 0\n",
    "        for i in range(len(nums)):                      \n",
    "            if nums[i] not in nums[i+1:]:\n",
    "                count += 1\n",
    "                nums[count-1] = nums[i]\n",
    "        return(count)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        nums[:] = list(set(nums))\n",
    "        nums.sort()\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if len(nums) == 0:\n",
    "            return 0\n",
    "        count = 1\n",
    "        s = 0\n",
    "        f = 1\n",
    "        while f < len(nums):\n",
    "            if nums[s] != nums[f]:\n",
    "                nums[count] = nums[f]\n",
    "                count += 1\n",
    "                s = f\n",
    "            f += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        i = 0\n",
    "        j = 1\n",
    "        length = len(nums)\n",
    "        while i < length and j < length:\n",
    "            if nums[i] == nums[j]:\n",
    "                length = length-1\n",
    "                nums.pop(j)\n",
    "            else:\n",
    "                i += 1\n",
    "                j += 1\n",
    "        new_length = len(nums)\n",
    "        return new_length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        new_nums=list(set(nums))\n",
    "        length=len(list(set(nums)))\n",
    "        if len(nums)>length:\n",
    "            nums[0:length]=sorted(new_nums)\n",
    "        return len(new_nums)\n",
    "        # if not nums:\n",
    "        #     return 0\n",
    "        # p = 1#position of the last unique element\n",
    "        # for i in range(1,len(nums)):\n",
    "        #     if nums[i] != nums[i-1]:\n",
    "        #         nums[p] = nums[i]\n",
    "        #         p += 1\n",
    "        # print(nums)\n",
    "        # print(p)\n",
    "        # return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        \n",
    "        \n",
    "        deleteDic = {}\n",
    "        keys = list(set(nums))\n",
    "        for i in keys:\n",
    "            count = 0\n",
    "            for k in nums:\n",
    "                if k == i:\n",
    "                    count += 1\n",
    "            deleteDic[i] = count\n",
    "            \n",
    "        for i in list(deleteDic.keys()):\n",
    "            for k in range(deleteDic[i] -1):\n",
    "                nums.remove(i)\n",
    "                \n",
    "        return len(keys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#\n",
    "# @lc app=leetcode.cn id=26 lang=python3\n",
    "#\n",
    "# [26] 删除排序数组中的重复项\n",
    "#\n",
    "# https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/\n",
    "#\n",
    "# algorithms\n",
    "# Easy (41.97%)\n",
    "# Total Accepted:    78K\n",
    "# Total Submissions: 185.8K\n",
    "# Testcase Example:  '[1,1,2]'\n",
    "#\n",
    "# 给定一个排序数组，你需要在原地删除重复出现的元素，使得每个元素只出现一次，返回移除后数组的新长度。\n",
    "# \n",
    "# 不要使用额外的数组空间，你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。\n",
    "# \n",
    "# 示例 1:\n",
    "# \n",
    "# 给定数组 nums = [1,1,2], \n",
    "# \n",
    "# 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 \n",
    "# \n",
    "# 你不需要考虑数组中超出新长度后面的元素。\n",
    "# \n",
    "# 示例 2:\n",
    "# \n",
    "# 给定 nums = [0,0,1,1,1,2,2,3,3,4],\n",
    "# \n",
    "# 函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。\n",
    "# \n",
    "# 你不需要考虑数组中超出新长度后面的元素。\n",
    "# \n",
    "# \n",
    "# 说明:\n",
    "# \n",
    "# 为什么返回数值是整数，但输出的答案是数组呢?\n",
    "# \n",
    "# 请注意，输入数组是以“引用”方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。\n",
    "# \n",
    "# 你可以想象内部操作如下:\n",
    "# \n",
    "# // nums 是以“引用”方式传递的。也就是说，不对实参做任何拷贝\n",
    "# int len = removeDuplicates(nums);\n",
    "# \n",
    "# // 在函数里修改输入数组对于调用者是可见的。\n",
    "# // 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。\n",
    "# for (int i = 0; i < len; i++) {\n",
    "# print(nums[i]);\n",
    "# }\n",
    "# \n",
    "# \n",
    "#\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        checkli={}\n",
    "        for i in nums:\n",
    "            k=str(i)\n",
    "            if k in checkli.keys():\n",
    "                checkli[k] +=1\n",
    "            else:\n",
    "                checkli[k]=0\n",
    "        for k ,v in checkli.items():\n",
    "            for i in range(v):\n",
    "                nums.remove(int(k))\n",
    "        return len(nums)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        d = dict()\n",
    "        for i in nums:\n",
    "            d[i] = d.get(i, 0) +1\n",
    "        for key, value in d.items():\n",
    "            if value > 1:\n",
    "                for j in range(value-1):\n",
    "                    nums.remove(key)\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        dic={}\n",
    "        i=0\n",
    "        while i<len(nums):\n",
    "            if nums[i] in dic:\n",
    "                nums.pop(i)\n",
    "                continue\n",
    "            else :\n",
    "                dic[nums[i]]=True\n",
    "                i+=1\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if (len(nums) == 0):\n",
    "            return 0\n",
    "        else:\n",
    "            i = 0\n",
    "            for j in range(1, len(nums)):\n",
    "                if (nums[j] != nums[i]):\n",
    "                    print(\"\")\n",
    "                    i += 1\n",
    "                    nums[i] = nums[j]\n",
    "            return i + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        for i,x in enumerate(nums):\n",
    "        \tif i == len(nums):\n",
    "        \t\tcontinue\n",
    "        \tfor a in nums[i+1:]:\n",
    "        \t\tif x == a:\n",
    "        \t\t\tdel nums[i]\n",
    "        return len(nums)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        curIndex=min(len(nums),1)\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i]!=nums[i-1]:\n",
    "                nums[curIndex]=nums[i]\n",
    "                curIndex+=1\n",
    "        return curIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        if not nums:\n",
    "            return 0\n",
    "        count=0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[count]!=nums[i]:\n",
    "                count += 1\n",
    "                nums[count]=nums[i]\n",
    "        return count+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if not nums: return 0\n",
    "        i = 0 \n",
    "        for j in range(1, len(nums)):\n",
    "            if nums[i] != nums[j]:\n",
    "                i += 1\n",
    "                nums[i] = nums[j]\n",
    "        return i + 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        i = 1\n",
    "        for j in range(len(nums)):\n",
    "            if j > 0 and nums[j] != nums[j - 1]:\n",
    "                nums[i] = nums[j]\n",
    "                i += 1\n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        slowIndex = 0\n",
    "        for fastIndex in range(len(nums)):\n",
    "            if(nums[slowIndex] != nums[fastIndex]):\n",
    "                slowIndex += 1\n",
    "                nums[slowIndex] = nums[fastIndex]\n",
    "                \n",
    "        return len(nums[:slowIndex+1])\n",
    "\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        for i in nums:\n",
    "            i_nums=nums.count(i)\n",
    "            while i_nums > 1:\n",
    "                nums.remove(i)\n",
    "                i_nums -= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "\n",
    "        n_set = len(set(nums))\n",
    "\n",
    "        for i in range(n_set):\n",
    "            c = nums[i]\n",
    "            while c in nums[i+1:]:\n",
    "                nums.remove(c)\n",
    "        return n_set\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        s = set(nums)\n",
    "        nums[:] = list(sorted(s))\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        #有序数组 nums ，请你 原地 删除重复出现的元素，使每个元素 只出现一次 ，返回删除后数组的新长度\n",
    "        #已按升序排序\n",
    "        #遍历中，l,r。nums[l]==nums[r]时，r+=1,l不变\n",
    "        #            nums[l]!=nums[r]时，l+=1,swap(num[l],num[r])\n",
    "        l,r=0,1\n",
    "        while r<len(nums):\n",
    "            if nums[l]==nums[r]:\n",
    "                r+=1\n",
    "            elif nums[l]!=nums[r]:\n",
    "                l+=1\n",
    "                t=nums[l]\n",
    "                nums[l]=nums[r]\n",
    "                nums[r]=t\n",
    "                r+=1\n",
    "        return l+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "\n",
    "        if n < 2:\n",
    "            return len(nums)\n",
    "\n",
    "        # if n == 2:\n",
    "        #     return len(nums) - 1 if nums[0] == nums[1] else len(nums)\n",
    "        \n",
    "        k = 0   # 当前插入元素的位置，或者结果序列末尾的下一个位置\n",
    "        i = 0   # 连续元素的开始位置\n",
    "        while i < n:\n",
    "            j = i + 1   # 寻找连续元素末尾的下一位置\n",
    "            while j < n and nums[j] == nums[i]:\n",
    "                j += 1\n",
    "                continue\n",
    "                \n",
    "            j -= 1  # j 是连续元素末尾的下一个位置，j - 1 则是连续元素的末尾位置\n",
    "            nums[k] = nums[j]   # 插入元素\n",
    "            i = j + 1   # i 指向下一个连续元素的开始位置\n",
    "\n",
    "            # # 上面三行代码与下面两行代码等价\n",
    "            # nums[k] = nums[j - 1]\n",
    "            # i = j\n",
    "            \n",
    "            k += 1  # k 指向下一个插入元素的位置\n",
    "        \n",
    "        # 最终，k 指向结果序列末尾的下一个位置，即 k 等于结果序列的长度\n",
    "        return k\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        read_list = []\n",
    "        i = 0\n",
    "        while i < len(nums):\n",
    "            print(i)\n",
    "            if nums[i] not in read_list:\n",
    "                read_list.append(nums[i])\n",
    "                print(read_list)\n",
    "            else:\n",
    "                nums.pop(i)\n",
    "                print(i)\n",
    "                i -= 1\n",
    "                print(i)\n",
    "                print('here')\n",
    "            i += 1\n",
    "\n",
    "                \n",
    "\n",
    "        return len(nums)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if len(nums) <= 1:\n",
    "            return len(nums)\n",
    "        slowIndex = 0\n",
    "        size = len(nums)\n",
    "        for fastIndex in range(1, size):\n",
    "            if nums[fastIndex] != nums[slowIndex]:\n",
    "                slowIndex += 1\n",
    "                nums[slowIndex] = nums[fastIndex]\n",
    "        return slowIndex + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        a = list(set(nums))\n",
    "        a.sort()\n",
    "        nums.clear()\n",
    "        nums.extend(a)\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        a = list(set(nums))\n",
    "        a.sort()\n",
    "        nums.clear()\n",
    "        nums.extend(a)\n",
    "        return len(nums)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        a = 0\n",
    "        b = 0\n",
    "        for a in range(len(nums)):\n",
    "            if nums[a] not in nums[:b]:\n",
    "                nums[b]=nums[a]\n",
    "                b+=1\n",
    "        nums[:] = nums[:b]         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        num_set = set()\n",
    "        first, second, n = 0, 0,len(nums)\n",
    "        while first < n and second < n:\n",
    "            \n",
    "            if nums[second] in num_set:\n",
    "                second += 1\n",
    "            else:\n",
    "                num_set.add(nums[second])\n",
    "                nums[first]=nums[second]\n",
    "                first+=1\n",
    "                second+=1\n",
    "            print(first, second)\n",
    "        return first\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "         low = 0\n",
    "         fast = 1\n",
    "         for fast in range(len(nums)):\n",
    "             if nums[fast] != nums[low]:\n",
    "                 low += 1\n",
    "                 nums[low] = nums[fast]\n",
    "         return low + 1\n",
    "\n",
    "     \n",
    "\n",
    "              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        for i, x in enumerate(sorted(set(nums))): nums[i] = x\n",
    "        return i + 1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        lookup = dict()\n",
    "        i = 0\n",
    "        for e in nums:\n",
    "            if e not in lookup:\n",
    "                lookup[e] = i\n",
    "                nums[i] = e\n",
    "                i += 1\n",
    "        \n",
    "        return i"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
